import { Component, OnInit,ChangeDetectorRef } from '@angular/core';
import { StorageService } from '../../service/storage.service'
import { CommonService } from '../../service/common.service'
import { Router } from '@angular/router'
import { EventService } from '../../service/event.service'
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-transfer',
  templateUrl: './transfer.page.html',
  styleUrls: ['./transfer.page.scss'],
})
export class TransferPage implements OnInit {
  public userInfo:any;
  public money:number;
  public other:string = '';
  public flag:boolean = false;
  public password:any;
  public show:boolean=false;
  public pwflag:boolean=false;
  
  constructor(private storage:StorageService,private alertController:AlertController,
    private common:CommonService,private changeDetectorRef:ChangeDetectorRef,private router:Router,private eventService:EventService) { }

  ngOnInit() {
    // this.common.getIdInfo()
    this.userInfo = this.common.getInfo()
  }

  // 弹出框
  async presentAlert() {
    const alert = await this.alertController.create({
        // cssClass: 'my-custom-class',
        header: '提示',
        // subHeader: 'Subtitle',
        message: `您确定转账给${this.other} <br> 转出金币为： ${this.money}个`,
        buttons: [
          {
            text: '取消',
            role: 'cancel'
          }, {
            text: '确定',
            handler: () => {
              this.show=false
              this.pwflag = true
            }
          }
        ]
    });
    await alert.present();
  }

  // 全部转出
  onTransfer(){
    this.money = this.userInfo.coin
    this.changeDetectorRef.detectChanges()
  }

  onblur(){
    var patrn = /^\d+(\.\d+)?$/;
    if(!patrn.exec(this.money+'')){
      this.common.presentToast("请输入正确的金额")
      this.money = undefined
    }
  }

  //  确认转出
  confirm(){
    if(this.money < 100){
      this.common.presentAlert("金额不能低于100")
      return false
    }
    if(this.money == null || this.money == undefined){
      this.common.presentAlert("金额不能为空")
      return false
    }

    if(this.other == null || this.other == ''){
      this.common.presentAlert("账户不能为空")
      return false
    }

    if(this.common.yzShouJi(this.other) == false){
      return false
    }

    this.presentAlert()

  }


  // this called every time when user changed the code
  onCodeChanged(code: string) {
  }

  // this called only if user entered full code
  onCodeCompleted(code: string) {
    
    // console.log(code)
    // debugger
    if(code == null || code == ''){
      this.common.presentAlert("密码不能为空")
      return false
    } 
    if(code.length < 6){
      this.common.presentAlert("密码不能少于6位数")
      return false
    } 
    var money;
      var number = (this.money.toString()).indexOf(".");
      if(number!= -1) {
        money = +this.money
        // console.log("有"+money)
      } else {
            money = +this.money
            // console.log(money)
      }

      var api = '/tbr/transfer'
      // console.log(this.money)
      var params = {
        ownerUserId:this.userInfo.userId,
        balance:money.toFixed(2),
        phone:this.other,
        paymentCode:code
      }
      this.common.post(api,params,false).then((res:any)=>{
          this.flag = false
          this.changeDetectorRef.detectChanges()
          if(res.code == 200){
            // console.log(1)
            this.show=false
            this.pwflag = false
            this.userInfo.coin = (this.userInfo.coin - this.money).toFixed(2)
            this.events()
              this.router.navigate(['/success'],{
                queryParams: {
                  title:"金币互转成功",
                  coin:this.money,
                  other:this.other,
                  url:'/tabs/tab3'
                }
              })
              this.money = null
              this.other = ''
              return false
          }else{
            // this.common.presentAlert(res.msg)
            this.pwflag = false
            return false
          }
          
      })
  }

  closepw(){
    this.pwflag = false
  }

  //通知用户中心更新用户信息
  events(){
    this.eventService.event.emit('userstate');
  }


}
